home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / perl5 / Math::Trig.z / Math::Trig
Encoding:
Text File  |  2002-10-03  |  14.5 KB  |  397 lines

  1.  
  2.  
  3.  
  4. MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))                                                    MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      Math::Trig - trigonometric functions
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.              use Math::Trig;
  13.  
  14.              $x = tan(0.9);
  15.              $y = acos(3.7);
  16.              $z = asin(2.4);
  17.  
  18.              $halfpi = pi/2;
  19.  
  20.              $rad = deg2rad(120);
  21.  
  22.  
  23. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  24.      Math::Trig defines many trigonometric functions not defined by the core
  25.      Perl which defines only the sin() and cos().  The constant ppppiiii is also
  26.      defined as are a few convenience functions for angle conversions.
  27.  
  28. TTTTRRRRIIIIGGGGOOOONNNNOOOOMMMMEEEETTTTRRRRIIIICCCC FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
  29.      The tangent
  30.  
  31.      ttttaaaannnn
  32.  
  33.      The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
  34.      are aliases)
  35.  
  36.      ccccsssscccc, ccccoooosssseeeecccc, sssseeeecccc, sssseeeecccc, ccccooootttt, ccccoooottttaaaannnn
  37.  
  38.      The arcus (also known as the inverse) functions of the sine, cosine, and
  39.      tangent
  40.  
  41.      aaaassssiiiinnnn, aaaaccccoooossss, aaaattttaaaannnn
  42.  
  43.      The principal value of the arc tangent of y/x
  44.  
  45.      aaaattttaaaannnn2222(y, x)
  46.  
  47.      The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and
  48.      acotan/acot are aliases)
  49.  
  50.      aaaaccccsssscccc, aaaaccccoooosssseeeecccc, aaaasssseeeecccc, aaaaccccooootttt, aaaaccccoooottttaaaannnn
  51.  
  52.      The hyperbolic sine, cosine, and tangent
  53.  
  54.      ssssiiiinnnnhhhh, ccccoooosssshhhh, ttttaaaannnnhhhh
  55.  
  56.      The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
  57.      and cotanh/coth are aliases)
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))                                                    MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  71.  
  72.  
  73.  
  74.      ccccsssscccchhhh, ccccoooosssseeeecccchhhh, sssseeeecccchhhh, ccccooootttthhhh, ccccoooottttaaaannnnhhhh
  75.  
  76.      The arcus (also known as the inverse) functions of the hyperbolic sine,
  77.      cosine, and tangent
  78.  
  79.      aaaassssiiiinnnnhhhh, aaaaccccoooosssshhhh, aaaattttaaaannnnhhhh
  80.  
  81.      The arcus cofunctions of the hyperbolic sine, cosine, and tangent
  82.      (acsch/acosech and acoth/acotanh are aliases)
  83.  
  84.      aaaaccccsssscccchhhh, aaaaccccoooosssseeeecccchhhh, aaaasssseeeecccchhhh, aaaaccccooootttthhhh, aaaaccccoooottttaaaannnnhhhh
  85.  
  86.      The trigonometric constant ppppiiii is also defined.
  87.  
  88.      $pi2 = 2 * ppppiiii;
  89.  
  90.      EEEERRRRRRRROOOORRRRSSSS DDDDUUUUEEEE TTTTOOOO DDDDIIIIVVVVIIIISSSSIIIIOOOONNNN BBBBYYYY ZZZZEEEERRRROOOO
  91.  
  92.      The following functions
  93.  
  94.              acoth
  95.              acsc
  96.              acsch
  97.              asec
  98.              asech
  99.              atanh
  100.              cot
  101.              coth
  102.              csc
  103.              csch
  104.              sec
  105.              sech
  106.              tan
  107.              tanh
  108.  
  109.      cannot be computed for all arguments because that would mean dividing by
  110.      zero or taking logarithm of zero. These situations cause fatal runtime
  111.      errors looking like this
  112.  
  113.              cot(0): Division by zero.
  114.              (Because in the definition of cot(0), the divisor sin(0) is 0)
  115.              Died at ...
  116.  
  117.      or
  118.  
  119.              atanh(-1): Logarithm of zero.
  120.              Died at...
  121.  
  122.      For the csc, cot, asec, acsc, acot, csch, coth, asech, acsch, the
  123.      argument cannot be 0 (zero).  For the atanh, acoth, the argument cannot
  124.      be 1 (one).  For the atanh, acoth, the argument cannot be -1 (minus one).
  125.      For the tan, sec, tanh, sech, the argument cannot be _p_i/_2 + _k * _p_i, where
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))                                                    MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  137.  
  138.  
  139.  
  140.      _k is any integer.
  141.  
  142.      SSSSIIIIMMMMPPPPLLLLEEEE ((((RRRREEEEAAAALLLL)))) AAAARRRRGGGGUUUUMMMMEEEENNNNTTTTSSSS,,,, CCCCOOOOMMMMPPPPLLLLEEEEXXXX RRRREEEESSSSUUUULLLLTTTTSSSS
  143.  
  144.      Please note that some of the trigonometric functions can break out from
  145.      the rrrreeeeaaaallll aaaaxxxxiiiissss into the ccccoooommmmpppplllleeeexxxx ppppllllaaaannnneeee. For example asin(2) has no
  146.      definition for plain real numbers but it has definition for complex
  147.      numbers.
  148.  
  149.      In Perl terms this means that supplying the usual Perl numbers (also
  150.      known as scalars, please see the _p_e_r_l_d_a_t_a manpage) as input for the
  151.      trigonometric functions might produce as output results that no more are
  152.      simple real numbers: instead they are complex numbers.
  153.  
  154.      The Math::Trig handles this by using the Math::Complex package which
  155.      knows how to handle complex numbers, please see the _M_a_t_h::_C_o_m_p_l_e_x manpage
  156.      for more information. In practice you need not to worry about getting
  157.      complex numbers as results because the Math::Complex takes care of
  158.      details like for example how to display complex numbers. For example:
  159.  
  160.              print asin(2), "\n";
  161.  
  162.      should produce something like this (take or leave few last decimals):
  163.  
  164.              1.5707963267949-1.31695789692482i
  165.  
  166.      That is, a complex number with the real part of approximately 1.571 and
  167.      the imaginary part of approximately -1.317.
  168.  
  169. PPPPLLLLAAAANNNNEEEE AAAANNNNGGGGLLLLEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS
  170.      (Plane, 2-dimensional) angles may be converted with the following
  171.      functions.
  172.  
  173.              $radians  = deg2rad($degrees);
  174.              $radians  = grad2rad($gradians);
  175.  
  176.              $degrees  = rad2deg($radians);
  177.              $degrees  = grad2deg($gradians);
  178.  
  179.              $gradians = deg2grad($degrees);
  180.              $gradians = rad2grad($radians);
  181.  
  182.      The full circle is 2 _p_i radians or _3_6_0 degrees or _4_0_0 gradians.
  183.  
  184. RRRRAAAADDDDIIIIAAAALLLL CCCCOOOOOOOORRRRDDDDIIIINNNNAAAATTTTEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS
  185.      RRRRaaaaddddiiiiaaaallll ccccoooooooorrrrddddiiiinnnnaaaatttteeee ssssyyyysssstttteeeemmmmssss are the sssspppphhhheeeerrrriiiiccccaaaallll and the ccccyyyylllliiiinnnnddddrrrriiiiccccaaaallll systems,
  186.      explained shortly in more detail.
  187.  
  188.      You can import radial coordinate conversion functions by using the
  189.      :radial tag:
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))                                                    MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  203.  
  204.  
  205.  
  206.          use Math::Trig ':radial';
  207.  
  208.          ($rho, $theta, $z)     = cartesian_to_cylindrical($x, $y, $z);
  209.          ($rho, $theta, $phi)   = cartesian_to_spherical($x, $y, $z);
  210.          ($x, $y, $z)           = cylindrical_to_cartesian($rho, $theta, $z);
  211.          ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
  212.          ($x, $y, $z)           = spherical_to_cartesian($rho, $theta, $phi);
  213.          ($rho_c, $theta, $z)   = spherical_to_cylindrical($rho_s, $theta, $phi);
  214.  
  215.      AAAAllllllll aaaannnngggglllleeeessss aaaarrrreeee iiiinnnn rrrraaaaddddiiiiaaaannnnssss.
  216.  
  217.      CCCCOOOOOOOORRRRDDDDIIIINNNNAAAATTTTEEEE SSSSYYYYSSSSTTTTEEEEMMMMSSSS
  218.  
  219.      CCCCaaaarrrrtttteeeessssiiiiaaaannnn coordinates are the usual rectangular (_x, _y, _z)-coordinates.
  220.  
  221.      Spherical coordinates, (_r_h_o, _t_h_e_t_a, _p_i), are three-dimensional
  222.      coordinates which define a point in three-dimensional space.  They are
  223.      based on a sphere surface.  The radius of the sphere is rrrrhhhhoooo, also known
  224.      as the _r_a_d_i_a_l coordinate.  The angle in the _x_y-plane (around the _z-axis)
  225.      is tttthhhheeeettttaaaa, also known as the _a_z_i_m_u_t_h_a_l coordinate.  The angle from the
  226.      _z-axis is pppphhhhiiii, also known as the _p_o_l_a_r coordinate.  The `North Pole' is
  227.      therefore _0, _0, _r_h_o, and the `Bay of Guinea' (think of the missing big
  228.      chunk of Africa) _0, _p_i/_2, _r_h_o.
  229.  
  230.      BBBBeeeewwwwaaaarrrreeee: some texts define _t_h_e_t_a and _p_h_i the other way round, some texts
  231.      define the _p_h_i to start from the horizontal plane, some texts use _r in
  232.      place of _r_h_o.
  233.  
  234.      Cylindrical coordinates, (_r_h_o, _t_h_e_t_a, _z), are three-dimensional
  235.      coordinates which define a point in three-dimensional space.  They are
  236.      based on a cylinder surface.  The radius of the cylinder is rrrrhhhhoooo, also
  237.      known as the _r_a_d_i_a_l coordinate.  The angle in the _x_y-plane (around the
  238.      _z-axis) is tttthhhheeeettttaaaa, also known as the _a_z_i_m_u_t_h_a_l coordinate.  The third
  239.      coordinate is the _z, pointing up from the tttthhhheeeettttaaaa-plane.
  240.  
  241.      3333----DDDD AAAANNNNGGGGLLLLEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS
  242.  
  243.      Conversions to and from spherical and cylindrical coordinates are
  244.      available.  Please notice that the conversions are not necessarily
  245.      reversible because of the equalities like _p_i angles being equal to -_p_i
  246.      angles.
  247.  
  248.      cartesian_to_cylindrical
  249.  
  250.                  ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
  251.  
  252.  
  253.      cartesian_to_spherical
  254.  
  255.                  ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))                                                    MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  269.  
  270.  
  271.  
  272.      cylindrical_to_cartesian
  273.  
  274.                  ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
  275.  
  276.  
  277.      cylindrical_to_spherical
  278.  
  279.                  ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
  280.  
  281.          Notice that when $z is not 0 $rho_s is not equal to $rho_c.
  282.  
  283.      spherical_to_cartesian
  284.  
  285.                  ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
  286.  
  287.  
  288.      spherical_to_cylindrical
  289.  
  290.                  ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
  291.  
  292.          Notice that when $z is not 0 $rho_c is not equal to $rho_s.
  293.  
  294. GGGGRRRREEEEAAAATTTT CCCCIIIIRRRRCCCCLLLLEEEE DDDDIIIISSSSTTTTAAAANNNNCCCCEEEESSSS
  295.      You can compute spherical distances, called ggggrrrreeeeaaaatttt cccciiiirrrrcccclllleeee ddddiiiissssttttaaaannnncccceeeessss, by
  296.      importing the great_circle_distance function:
  297.  
  298.              use Math::Trig 'great_circle_distance'
  299.  
  300.          $distance = great_circle_distance($theta0, $phi0, $theta1, $phi, [, $rho]);
  301.  
  302.      The _g_r_e_a_t _c_i_r_c_l_e _d_i_s_t_a_n_c_e is the shortest distance between two points on
  303.      a sphere.  The distance is in $rho units.  The $rho is optional, it
  304.      defaults to 1 (the unit sphere), therefore the distance defaults to
  305.      radians.
  306.  
  307. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  308.      To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N
  309.      139.8E) in kilometers:
  310.  
  311.              use Math::Trig qw(great_circle_distance deg2rad);
  312.  
  313.              # Notice the 90 - latitude: phi zero is at the North Pole.
  314.              @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
  315.              @T = (deg2rad(139.8),deg2rad(90 - 35.7));
  316.  
  317.              $km = great_circle_distance(@L, @T, 6378);
  318.  
  319.      The answer may be off by up to 0.3% because of the irregular (slightly
  320.      aspherical) form of the Earth.
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))                                                    MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  335.  
  336.  
  337.  
  338. BBBBUUUUGGGGSSSS
  339.      Saying use Math::Trig; exports many mathematical routines in the caller
  340.      environment and even overrides some (sin, cos).  This is construed as a
  341.      feature by the Authors, actually... ;-)
  342.  
  343.      The code is not optimized for speed, especially because we use
  344.      Math::Complex and thus go quite near complex numbers while doing the
  345.      computations even when the arguments are not. This, however, cannot be
  346.      completely avoided if we want things like asin(2) to give an answer
  347.      instead of giving a fatal runtime error.
  348.  
  349. AAAAUUUUTTTTHHHHOOOORRRRSSSS
  350.      Jarkko Hietaniemi <_j_h_i@_i_k_i._f_i> and Raphael Manfredi
  351.      <_R_a_p_h_a_e_l__M_a_n_f_r_e_d_i@_g_r_e_n_o_b_l_e._h_p._c_o_m>.
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.